package com.ycsj.common.map;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * 创建日期：2017/9/30  15:02
 * 描述:封装的地图定位功能
 * 作者:张伟
 */

public class LocationManager {

    private static final String TAG = LocationManager.class.getName();
    private static LocationManager instance;
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private LocationResultListener locationResultListener;

    public void setLocationResultListener(LocationResultListener locationResultListener) {
        this.locationResultListener = locationResultListener;
    }

    public static LocationManager getInstance() {
        if (null == instance) {
            synchronized (LocationManager.class) {
                if (null == instance) {
                    instance = new LocationManager();
                }
            }
        }
        return instance;
    }

    //开始定位
    public void start(Context context) {
        // 设置定位监听
        locationClient = new AMapLocationClient(context.getApplicationContext());
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位次数   一次  仅支持 高精度定位
        locationOption.setOnceLocation(true);
        //设置定位参数
        locationClient.setLocationOption(locationOption);
        locationClient.setLocationListener(listener);
        locationClient.startLocation();
    }

    //停止定位
    public void stop() {
        if (null != locationClient) {
            locationClient.stopLocation();
            locationClient.unRegisterLocationListener(listener);
        }
    }

    public void destroy() {
        if (null != locationClient) {
            /*** 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             *
             *
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 取消监听*/

            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }

    private AMapLocationListener listener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation loc) {
            if (null != loc && null != locationResultListener) {
                Log.e(TAG, loc.toString());
                LocationResultEntity entity = new LocationResultEntity();
                entity.province = loc.getProvince();
                entity.errorCode = loc.getErrorCode();
                entity.errorInfo = loc.getErrorInfo();
                entity.latitude = loc.getLatitude();
                entity.longitude = loc.getLongitude();
                entity.cityId = loc.getCityCode();
                entity.cityName = loc.getCity();
                entity.district = loc.getDistrict();
                entity.address = loc.getAddress();
                entity.aMapLocation = loc;
                entity.road = loc.getStreet();
                entity.aoiName = loc.getAoiName();
                locationResultListener.locationListener(entity);
            }
        }
    };
}
